/******************************************************************
*
*	CyberUPnP for Java
*
*	Copyright (C) Satoshi Konno 2002-2003
*
*	File: SSDPNotifySocket.java
*
*	Revision;
*
*	11/20/02
*		- first revision.
*	05/13/03
*		- Added support for IPv6.
*	02/20/04
*		- Inma Marin Lopez <inma@dif.um.es>
*		- Added a multicast filter using the SSDP pakcet.
*	
******************************************************************/

package org.cybergarage.upnp.ssdp;

import java.net.*;
import java.util.logging.Logger;

import org.cybergarage.net.*;
import org.cybergarage.util.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.*;

public class SSDPNotifySocket extends HTTPMUSocket implements Runnable
{
  private static Logger logger = Logger.getLogger("org.cybergarage.upnp.ssdp");

	private boolean useIPv6Address;
	
	////////////////////////////////////////////////
	//	Constructor
	////////////////////////////////////////////////
	
	public SSDPNotifySocket(String bindAddr)
	{
		String addr = SSDP.ADDRESS;
		useIPv6Address = false;
		if (HostInterface.isIPv6Address(bindAddr) == true) {
      logger.warning("IPv6 BindAddr detected: " + bindAddr );
			addr = SSDP.getIPv6Address();
			useIPv6Address = true;
		}
		open(addr, SSDP.PORT, bindAddr);
		setControlPoint(null);
	}

	////////////////////////////////////////////////
	//	ControlPoint	
	////////////////////////////////////////////////

	private ControlPoint controlPoint = null;
	
	public void setControlPoint(ControlPoint ctrlp)
	{
		this.controlPoint = ctrlp;
	}

	public ControlPoint getControlPoint()
	{
		return controlPoint;
	}

	////////////////////////////////////////////////
	//	post (SSDPNotifySocket)
	////////////////////////////////////////////////

	public boolean post(SSDPNotifyRequest req)
	{
		String ssdpAddr = SSDP.ADDRESS;
		if (useIPv6Address == true)
			ssdpAddr = SSDP.getIPv6Address();
		req.setHost(ssdpAddr, SSDP.PORT);
		return post((HTTPRequest)req);
	}

	////////////////////////////////////////////////
	//	run	
	////////////////////////////////////////////////

	private Thread deviceNotifyThread = null;
		
	public void run()
	{
		Thread thisThread = Thread.currentThread();
		
		ControlPoint ctrlPoint = getControlPoint();
		
		while (deviceNotifyThread == thisThread) {
			Thread.yield();
			SSDPPacket packet = receive();
			
			// Thanks for Inma (02/20/2004)
			InetAddress maddr = getMulticastInetAddress();
			InetAddress pmaddr = packet.getHostInetAddress();
			if (maddr.equals(pmaddr) == false) {
				logger.warning("Invalidate Multicast Received : " + maddr + "," + pmaddr);
				continue;
			}
												
			if (ctrlPoint != null)
				ctrlPoint.notifyReceived(packet); 
		}
	}
	
	public void start()
	{
		deviceNotifyThread = new Thread(this);
		deviceNotifyThread.start();
	}
	
	public void stop()
	{
		deviceNotifyThread = null;
	}
}

